package com.xxxy.jsq.action.level;

import com.alibaba.fastjson2.JSONObject;
import com.xxxy.jsq.dao.LevelDao;
import com.xxxy.jsq.model.Level;
import com.xxxy.jsq.model.Page;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class GetAllLevelsPageServlet
 */
@WebServlet("/getAllLevelsPageServlet")
public class getAllLevelsPageServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public getAllLevelsPageServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        Level model = new Level();
        LevelDao dao = new LevelDao();

        // 获取页面参数
        String cflag = request.getParameter("cflag");
        String cname = request.getParameter("cname");
        String pid = request.getParameter("pnames");

        if (cflag != null && !cflag.equals("")) {
            model.setLevelFlag(cflag);
        }
        if (cname != null && !cname.equals("")) {
            model.setLevelName(cname);
        }
        if (pid != null && !pid.equals("")) {
            model.setLevelId(Long.valueOf(pid));
        }

        String cpage = request.getParameter("page");
        String limit = request.getParameter("limit");

        Page page = new Page();
        if (cpage != null && !cpage.equals("") && limit != null && !limit.equals("")) {
            try {
                page.setCurrentPage(Integer.parseInt(cpage));
                page.setCount(Integer.parseInt(limit));
            } catch (NumberFormatException e) {
                // 参数非法处理
                JSONObject jo = new JSONObject();
                jo.put("code", 1);
                jo.put("msg", "参数错误");
                jo.put("count", 0);
                jo.put("data", new ArrayList<>());

                PrintWriter out = response.getWriter();
                out.print(jo.toString());
                out.flush();
                out.close();
                return;
            }
        }

        List<Level> list = dao.getAllDataByPage(model, page);
        int total = dao.getCount(model);

        JSONObject jo = new JSONObject();
        jo.put("code", 0);
        jo.put("msg", "你好");
        jo.put("count", total);
        jo.put("data", list);

        PrintWriter out = response.getWriter();
        out.print(jo.toString());
        out.flush();
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
